package com.dongjiantong.app.controller.biz;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongjiantong.biz.model.EarMarkLife;
import com.dongjiantong.biz.service.EarMarkLifeReadeService;
import com.dongjiantong.biz.service.EarMarkLifeWriteService;
import com.dongjiantong.common.exceptions.HttpCode;
import com.dongjiantong.common.exceptions.OPServerException;
import com.dongjiantong.common.model.Response;
import com.dongjiantong.openplatform.annotations.OpenBean;
import com.dongjiantong.openplatform.annotations.OpenMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMethod;


/**
 * Created by ldf   on 2017/11/17.
 */
@Slf4j
@OpenBean
public class EarMarkLifeController {

    @Reference(version = "1.0.0")
    private EarMarkLifeWriteService earMarkLifeWriteService;
    @Reference(version = "1.0.0")
    private EarMarkLifeReadeService earMarkLifeReadeService;

    /**
     *  创建耳标记录表
     *
     * @param earMarkLife 耳标实体类
     * @return
     */
    @OpenMethod(key = "app.EarMarkLife.createEarMarkLife",
            paramNames = {"earMarkLife"},
            httpMethods = RequestMethod.POST)
    @Transactional
    public Response<String> createEarMarkLife(EarMarkLife earMarkLife) {
        if (earMarkLife == null) {
            throw new OPServerException(HttpCode.DATA_CREATE_FAIL, "参数为null");
        }

        int result = earMarkLifeReadeService.insertSelective(earMarkLife);
        if (result == 0) {
            throw new OPServerException(HttpCode.DATA_CREATE_FAIL, "插入失败请稍后再试");
        }
        return Response.ok("创建成功");
    }

    /**
     * todo 根据id查询
     *
     * @param id 主键
     * @return
     */
    @OpenMethod(key = "app.EarMarkLife.findById",
            paramNames = {"id"},
            httpMethods = RequestMethod.POST)
    public Response<EarMarkLife> findById(Long id) {
        if (id == null) {
            throw new OPServerException(HttpCode.BAD_REQUEST, "id丢失");
        }
        EarMarkLife earMarkLife = earMarkLifeReadeService.selectByPrimaryKey(id);
        if (earMarkLife == null) {
            throw new OPServerException(HttpCode.INTERNAL_SERVER_ERROR, "没有此Id");
        }
        return Response.ok(earMarkLife);
    }

    /**
     * todo 根据关联id查询 耳标一生
     *
     * @param report_id   关联id
     * @param report_type 关联单据类型 无害化,屠宰,检疫…
     * @return
     */
    @OpenMethod(key = "app.EarMarkLife.findByReportId",
            paramNames = {"report_id","report_type"},
            httpMethods = RequestMethod.POST)
    public Response<EarMarkLife> findByReportId(Long report_id, Integer report_type) {
        if (report_id == null) {
            throw new OPServerException(HttpCode.BAD_REQUEST, "report_id丢失");
        }
        if (report_type == null) {
            throw new OPServerException(HttpCode.BAD_REQUEST, "report_type丢失");
        }
        EarMarkLife earMarkLife1 = new EarMarkLife();
        earMarkLife1.setReport_id(report_id);
        earMarkLife1.setReport_type(report_type);

        try {
            EarMarkLife earMarkLife = earMarkLifeReadeService.selectOne(earMarkLife1);
            if (earMarkLife == null) {
                throw new OPServerException(HttpCode.INTERNAL_SERVER_ERROR, "没有查询到");
            }
            return Response.ok(earMarkLife);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new OPServerException(HttpCode.INTERNAL_SERVER_ERROR, "查询错误请稍后再试");
        }

    }

}
